#import "mfInteractiveView.h"

@implementation mfInteractiveView

//------------------------------------------------------
- (id) initWithFrame:(CGRect)frame
{
	return [self initWithFrame:frame pixelFormat:GL_RGB565_OES depthFormat:0 preserveBackbuffer:NO];
}

- (id) initWithFrame:(CGRect)frame pixelFormat:(GLuint)format 
{
	return [self initWithFrame:frame pixelFormat:format depthFormat:0 preserveBackbuffer:NO];
}

- (id) initWithFrame:(CGRect)frame pixelFormat:(GLuint)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained
{
	self = [super initWithFrame:frame pixelFormat:format depthFormat:depth preserveBackbuffer:retained];
	
	self.multipleTouchEnabled = true;
	
	return self;
}

//------------------------------------------------------
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	
	NSArray *all = [touches allObjects];
	int count = [all count];
	NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
	for(int i=0; i<count; i++){
		
		// find a new touch
		UITouch *touch = [all objectAtIndex:i];
		if([touch phase] != UITouchPhaseBegan)
			continue;
		
		// place it in an empty space in the array
		int index=0;
		while( index<MF_MAX_TOUCHES && touchesExist[index] )
			index++;
		if(index==MF_MAX_TOUCHES)
			index=0;
		
		touchesExist[index] = true;
		activeTouches[index] = touch;
		
		CGPoint touchPoint = [touch locationInView:self];
		
		if( index==0 ){
			[mfAppRunner getTouchApp]->mouseX = touchPoint.x;
			[mfAppRunner getTouchApp]->mouseY = touchPoint.y;
		}
		
		printf("touched with %i",index);
		
		// was it a double touch?
		if( [touch tapCount] == 2 )
			[mfAppRunner getTouchApp]->doubleTap(touchPoint.x, touchPoint.y);
		else
			[mfAppRunner getTouchApp]->touchDown(touchPoint.x, touchPoint.y, index);
		
	}
	[pool release];
}

//------------------------------------------------------
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	
	NSArray *all = [touches allObjects];
	int count = [all count];
	NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
	for(int i=0; i<count; i++){
		
		// find a moved touch
		UITouch *touch = [all objectAtIndex:i];
		if([touch phase] != UITouchPhaseMoved)
			continue;
		
		// find it in the list!
		int index=0;
		while( index<MF_MAX_TOUCHES && (!touchesExist[index] || activeTouches[index]!=touch) )
			index++;
		if( index==MF_MAX_TOUCHES )
			continue;
		
		CGPoint touchPoint = [touch locationInView:self];
		
		if( index==0 ){
			[mfAppRunner getTouchApp]->mouseX = touchPoint.x;
			[mfAppRunner getTouchApp]->mouseY = touchPoint.y;
		}
		
		[mfAppRunner getTouchApp]->touchMove(touchPoint.x, touchPoint.y, index);
	}
	[pool release];
}

//------------------------------------------------------
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	NSArray *all = [touches allObjects];
	int count = [all count];
	NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
	for(int i=0; i<count; i++){
		
		// find a finished touch
		UITouch *touch = [all objectAtIndex:i];
		if([touch phase] != UITouchPhaseEnded)
			continue;
		
		// find it in the list!
		int index=0;
		while( index<MF_MAX_TOUCHES && (!touchesExist[index] || activeTouches[index]!=touch) )
			index++;
		if( index==MF_MAX_TOUCHES )
			continue;
		
		touchesExist[index] = false;
		
		CGPoint touchPoint = [touch locationInView:self];
		
		if( index==0 ){
			[mfAppRunner getTouchApp]->mouseX = touchPoint.x;
			[mfAppRunner getTouchApp]->mouseY = touchPoint.y;
		}
		
		[mfAppRunner getTouchApp]->touchUp(touchPoint.x, touchPoint.y, index);
	}
	[pool release];
}

//------------------------------------------------------
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	// end all active touches
	for(int i=0; i<MF_MAX_TOUCHES; i++){
		if(touchesExist[i]){
			touchesExist[i] = false;
			
			UITouch * touch = activeTouches[i];
			
			CGPoint touchPoint = [touch locationInView:self];
			
			if( i==0 ){
				[mfAppRunner getTouchApp]->mouseX = touchPoint.x;
				[mfAppRunner getTouchApp]->mouseY = touchPoint.y;
			}
			
			[mfAppRunner getTouchApp]->touchUp(touchPoint.x, touchPoint.y, i);
		}
	}
}

@end
